<?php
  require_once('./db.php');
  $id = isset($_REQUEST['id'])?$_REQUEST['id']:0;
pr($_FILES);
pr($_POST);

    if(isset($_POST['name']) && !empty($_POST['name'])) {
      ###############################
      // $rules is in db.php
      $uploadArray = uploadFile('avatar',$rules);

      $renamedName =  $uploadArray['renamedName'];
      $msg =  $uploadArray['msg'];
      $flag =  $uploadArray['flag'];

      if($flag==true && $renamedName!= '' ) {
        $_POST['avatar'] = $renamedName;

        require_once('thumbnail/image.class.php');
        $img = new Zubrag_image;
        $thumb_dir = './uploads/thumbs';
if(is_file($thumb_dir.'/'.$_POST['cur_avatar'])) {
        unlink($thumb_dir.'/'.$_POST['cur_avatar']);
}
if(is_file('./uploads/'.$_POST['cur_avatar'])) {
        unlink('./uploads/'.$_POST['cur_avatar']);
}

        if(!is_dir($thumb_dir)) {
          mkdir($thumb_dir,0755);
        }
        $img->max_x        = 100;
        $img->max_y        = 100;

        $img->GenerateThumbFile("./uploads/$renamedName", "$thumb_dir/$renamedName");
      }// end if flag true (file uploaded)
      ###############################
    pr($_POST);

    $_POST = array_map('trim',$_POST);
    $_POST = array_map('mysql_real_escape_string',$_POST);
    $_POST = array_map('strip_tags',$_POST);
    unset($_POST['cur_avatar']);

    $query = "UPDATE `users` SET ";
    $tmp = '';
    foreach($_POST as $colName=>$value) {
      $tmp .= "`$colName`='$value' ,";
    }
    $tmp = rtrim($tmp, ' ,');
    $where = " WHERE `id`='$id'";
    $query = $query.$tmp.$where;
    pq($query);
    mysql_query($query) or die(mysql_error());

    if(mysql_affected_rows() == 1) {
      echo "Record updated on ID <strong></strong>$id</strong>";
    }

  }

  $selectQuery = "SELECT * FROM `users`
                WHERE `id`='$id'";
  $userResult = mysql_query($selectQuery)or die(mysql_error());
  pq($selectQuery);
  $user = mysql_fetch_assoc($userResult);
  pr($user);
  $isActive = $user['status']==1?'checked':'';
  $isInactive = $user['status']==0?'checked':'';

  if($msg != '') {
    echo '<h4 style="background:yellow;border:2px solid pink;"><em>'.$msg.'</em></h4>';
  }
?>
<form action="" enctype="multipart/form-data" method="post">
  <input type="hidden" name="id" value="<?=$id?>" />
  <input type="text" name="cur_avatar" value="<?=$user['avatar']?>" />
  <table  border="1" cellpadding="0" cellspacing="0" width="80%" align="center">
    <thead>
      <tr>
        <th colspan="2">Please edit user</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td align="right">Name :</td>
        <td><input required type="text" name="name" value="<?=$user['name']?>" /></td>
      </tr>
      <tr>
        <td align="right">Email :</td>
        <td><input required type="email" name="email" value="<?=$user['email']?>"/></td>
      </tr>
      <tr>
        <td align="right">Status :</td>
        <td>

          <label><input value="0" required type="radio" name="status" <?=$isInactive?>/>Inactive</label>

          <label><input value="1" required type="radio" name="status" <?=$isActive?>/>Active</label>

          </td>
      </tr>

      <tr>
        <td align="right">Avatar :</td>
        <td><input type="file" name="avatar" />
        <img src="./uploads/thumbs/<?=$user['avatar']?>" alt="User Image" title="This is avatar." />
        </td>
      </tr>


      <tr>
        <td align="right">&nbsp;</td>
        <td><input type="submit" value="Update it!!" /></td>
      </tr>
    </tbody>
  </table>

</form>
<?php
  require_once('footer.php');
?>
